<?php

namespace app\seller\controller;

use app\common\controller\Seller;
use app\common\model\Company;
use think\Request;
use app\common\logic\SpreadLogic;
use think\Config;
class Spread extends Seller
{
    //显示视图以及搜索
    public function index(){
        
        $logic = new SpreadLogic();
        
        $params = paramForm();
        
        if(isset($params['keyword'])){
            $sql = ['word'=>$params['keyword'],'with'=>'user'];//获取所有相关的记录
            $logic->setSql($sql);
            $list = $logic->prepareDataProvider();
            if(count($list)<1){
                $list = null;
            }
            $this->assign('list',$list);
            $this->assign('keyword',$params['keyword']);
            return $this->fetch();
        }else{
            
            $this->assign('keyword',null);
            $this->assign('list',null);
            return $this->fetch();
        }
        
    }
    
    
    public function buy($keyword){
        
        
        $company = Company::get(SID);
        $this->assign('word',$keyword);
        $this->assign('company',$company);
        $this->assign('min',config('spread.min'));
        return $this->fetch();
        
    }
    
    public function my(){
        
        $logic = new SpreadLogic();
        $params = paramForm();
        if(Request::instance()->isAjax()){
            $params['user_id'] = SID;
            $logic->setSql($params);
            $list = $logic->prepareDataProvider();
            success($list);
        }else{
            return $this->fetch();
        }
        

    }
    
    public function doBuy(){
        $params = paramForm();
        $logic = new SpreadLogic();
        $params['endtime'] = strtotime($params['totime'])+(int)$params['month']*30*24*3600;
        $addRT = $logic->add($params);
    
        if(!$addRT['code']){
            $this->error($addRT['msg'],'/spread');
            die;
        }
    
        $this->success('申请提交成功,等待管理员审核','/spread');
        
    }
}
